package com.example.databinding.viewModel;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

public class ScoreViewModel extends ViewModel {

    private MutableLiveData<Integer> aTeamScore;
    private MutableLiveData<Integer> bTeamScore;
    private Integer lastAScore;
    private Integer lastBScore;

    //设置两队分数的get方法 用于取出变化的分数
    public MutableLiveData<Integer> getaTeamScore() {
        if (aTeamScore == null) {
            aTeamScore = new MutableLiveData<>();
            aTeamScore.setValue(0);
        }
        return aTeamScore;
    }

    public MutableLiveData<Integer> getbTeamScore() {
        if (bTeamScore == null) {
            bTeamScore = new MutableLiveData<>();
            bTeamScore.setValue(0);
        }
        return bTeamScore;
    }

    public void addAScore(int i) {
        remLastScoreA();
        aTeamScore.setValue(aTeamScore.getValue() + i);
    }

    public void addBScore(int i) {
        remLastScoreB();
        bTeamScore.setValue(bTeamScore.getValue() + i);
    }

    //重置方法
    public void resetA() {
        //重置时将上次记录的比分更新
        remLastScoreA();
        //重置比分
        aTeamScore.setValue(0);
    }

    public void resetB() {
        remLastScoreB();
        bTeamScore.setValue(0);
    }

    //重做方法
    public void undoA() {
        aTeamScore.setValue(lastAScore);
    }
    public void undoB() {
        bTeamScore.setValue(lastBScore);
    }

    //记录上一次的分数
    public void remLastScoreA() {
        this.lastAScore = aTeamScore.getValue();
    }

    public void remLastScoreB() {
        this.lastBScore = bTeamScore.getValue();
    }
}
